package com.example.demo.service;

import com.example.demo.entity.pojo.Card;
import com.example.demo.entity.pojo.CostRecord;
import com.example.demo.entity.vo.CostRecordVo;
import com.example.demo.mapper.CostRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CostRecordService {
    @Autowired
    CostRecordMapper costRecordMapper;
    @Autowired
    CardService cardService;
    /**
     * 添加流水纪录
     * @return
     */
    public boolean addCostRecord(double money,int cardId,int state){
        int i = costRecordMapper.addRecord(money, cardId,state);
        return i > 0;
    }


    /**
     * 查看流水纪录
     * @param cid
     * @param type
     * @return
     */
    public List<CostRecordVo> getRecord(int cid,int type){
        Card message = cardService.getMessage(cid);
        List<CostRecord> costRecords = new ArrayList<>();
        if (type == 10){
            costRecords = costRecordMapper.getAllRecord(cid);
        }else {
            costRecords = costRecordMapper.getRecordByType(cid,type);
        }
        List<CostRecordVo> list = new ArrayList<>();
        int i = 1;
        for (CostRecord costRecord : costRecords) {
            CostRecordVo costRecordVo = new CostRecordVo(costRecord,message);
            costRecordVo.setId(i);
            i++;
            list.add(costRecordVo);
        }
        return list;

    }
}
